<template>
  <div class="dashboard-container">
    <TitleBar title="编辑支付方式" refresh />
    <div class="quyu">
      <div class="set">
        <el-form
          ref="form"
          :label-position="labelPosition"
          :rules="xinForm"
          :model="form"
          label-width="220px"
        >
          <el-form-item prop="name" :required="true" label="支付方式名称:">
            <el-input v-model="form.name" this.a="false" />
          </el-form-item>
          <el-form-item prop="describe" :required="true" label="支付方式描述">
            <el-input
              class="textarea"
              type="textarea"
              rows="5"
              v-model="form.describe"
              placeholder="请输入内容"
            />
          </el-form-item>
          <el-form-item prop="APPID" :required="true" label="APPID:">
            <el-input :disabled="true" v-model="form.APPID" this.b="false" />
          </el-form-item>
          <el-form-item prop="APPSERCT" :required="true" label="APPSERCT:">
            <el-input v-model="form.APPSERCT" />
          </el-form-item>
          <el-form-item prop="KEY" :required="true" label="商户支付密钥KEY:">
            <el-input v-model="form.KEY" />
          </el-form-item>
          <el-form-item prop="business" :required="true" label="受理商ID(即微信支付商户号):">
            <el-input :disabled="true" v-model="form.business" />
          </el-form-item>
          <el-form-item prop="serviceCharge" :required="true" label="支付手续费:">
            <el-input v-model="form.serviceCharge" />
          </el-form-item>
          <el-form-item label="在线支付:">
            <template>
              <el-radio v-model="radio" label="1">是</el-radio>
              <el-radio v-model="radio" label="2">否</el-radio>
            </template>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="but">
      <el-button type="success" @click="redact">保存</el-button>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getRedact, getDetails } from "../../../api/setting.js";
import { constants } from "crypto";
import config from "@/settings";
export default {
  name: "Dashboard",
  data() {
    return {
      initList: [],
      radio: "1",
      uploadUrl: config.uploadUrl,
      fileList: [],
      Newsurl: "",
      token: {
        type: "6",
        whether: false
      },
      labelPosition: "left",
      xinList: [],
      form: {
        name: "",
        describe: "",
        APPID: "",
        APPSERCT: "",
        KEY: "",
        business: "",
        serviceCharge: "",
        pay: ""
      },
      xinForm: {
        name: [
          { required: true, message: "请输入平台名称", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (/^[\u4e00-\u9fa5_a-zA-Z0-9_]{2,30}$/.test(value) == false) {
                callback(new Error("平台名称由2-30位组成"));
              } else {
                ("blur");
                callback();
              }
            },
            trigger: "blur"
          }
        ],
        describe: [
          { required: true, message: "请输入支付方式描述", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (/^[\u4e00-\u9fa5_a-zA-Z0-9_]{2,30}$/.test(value) == false) {
                callback(new Error("描述由2-30位组成"));
              } else {
                ("blur");
                callback();
              }
            },
            trigger: "blur"
          }
        ],
        APPID: [
          { required: true, message: "请输入APPID", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (/^[_a-zA-Z0-9_]{2,30}$/.test(value) == false) {
                callback(new Error("APPID2-30位组成"));
              } else {
                ("blur");
                callback();
              }
            },
            trigger: "blur"
          }
        ],
        APPSERCT: [
          { required: true, message: "请输入APPSERCT", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (/^[_a-zA-Z0-9_]{2,30}$/.test(value) == false) {
                callback(new Error("APPSERCT2-30位组成"));
              } else {
                ("blur");
                callback();
              }
            },
            trigger: "blur"
          }
        ],
        KEY: [
          { required: true, message: "请输入密钥KEY！", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (/^[a-zA-Z0-9_].{2,60}$/.test(value) == false) {
                callback(new Error("关键词由2-60位数字字母特殊符号组成"));
              } else {
                ("blur");
                callback();
              }
            },
            trigger: "blur"
          }
        ],
        business: [
          { required: true, message: "请输入支付商户号！", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (/^[a-zA-Z0-9_].{2,30}$/.test(value) == false) {
                callback(new Error("关键词由2-30位数字字母特殊符号组成"));
              } else {
                ("blur");
                callback();
              }
            },
            trigger: "blur"
          }
        ],
        serviceCharge: [
          { required: true, message: "请输入支付手续费", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (/^[0-9_]{1,8}$/.test(value) == false) {
                callback(new Error("内容由1-8位数字组成"));
              } else {
                ("blur");
                callback();
              }
            },
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getDetails({
        payTypeCode: this.$route.query.code
      }).then(res => {
        this.dataList = res.data;
        if (res.code == 200) {
          this.form.name = res.data.payTypeName;
          this.form.describe = res.data.describes;
          this.form.APPID = res.data.appId;
          this.form.APPSERCT = res.data.appSecret;
          this.form.KEY = res.data.payKey;
          this.form.business = res.data.payTypeCode;
          this.form.serviceCharge = res.data.serviceCharge;
          this.radio = res.data.olState;
        }
      });
    },
    redact() {
      getRedact({
        appId: this.form.APPID,
        appSecret: this.form.APPSERCT,
        describes: this.form.describe,
        olState: this.radio,
        payKey: this.form.KEY,
        payTypeCode: this.form.business,
        payTypeName: this.form.name,
        serviceCharge: this.form.serviceCharge,
        merchantCode: this.dataList.merchantCode,
        payType: this.dataList.payType,
        id: this.dataList.id
      }).then(res => {
        if (res.code == 200) {
          this.$router.push("/setting/payMents");
          this.$message({
            message: "修改成功，将返回支付设置页面!",
            type: "success"
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.dashboard-container {
  padding: 0 80px 0 0;
  .el-input {
    width: 530px;
    height: 40px;
  }
  .textarea {
    width: 530px;
  }
  .but {
    margin-top: 40px;
    text-align: center;
    button {
      border: none;
      width: 106px;
      height: 40px;
      background: rgba(72, 141, 255, 1);
      border-radius: 4px;
    }
  }
  .quyu {
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1);
    border-radius: 4px;
    .el-form{
      height: auto;
      padding-bottom: 20px;
    }
  }
}
</style>
